package at.fhooe.mc.master.clc.server;

import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

import at.fhooe.mc.master.clc.client.AuthenticationService;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;


/**
 * Authentication service that checks if a user is logged in through the google 
 * user service.
 * 
 * @author Daniel Karzel
 *
 */
public class AuthenticationServiceImpl extends RemoteServiceServlet implements AuthenticationService
{
	private static final Logger log = Logger.getAnonymousLogger();
	private static final long serialVersionUID = 1L;

	@Override
	public String authenticate()
	{
		UserService service = UserServiceFactory.getUserService();
		
//		System.out.println(service.getCurrentUser());
		
		User user = service.getCurrentUser();
		
		// to restrict to admin add: && service.isUserAdmin() and activate the commented code below
		if (user != null && service.isUserLoggedIn())
		{
			log.log(Level.INFO, "User " + user.getEmail() + " loggin in at: " + new Date());
			return "OK";
		}
//		else if (user != null && service.isUserLoggedIn())
//		{
//			// in this case the user is logged in but not allowed to 
//			// view the page
//			return "NO_PERMISSION";
//		}
		
		String loginUrl = service.createLoginURL("/DatastoreMetrics.html");
		
		return loginUrl;
	}

}
